package com.chen.service;

import com.chen.dao.FanMapper;
import com.chen.dto.PageData;
import com.chen.entity.Fan;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

@Service
@Transactional
public class FanService {

    @Autowired
    private FanMapper fanMapper;


    //添加风车
    public boolean insertFan(Fan fan) {
        int i = this.fanMapper.insertSelective(fan);
        return i > 0;
    }


    //查询风机数据
    public PageData queryFans(int pageNum, int pageSize, String fjbh, String projectid, String btime, String etime,String uid) {
        if(pageNum != 0 &&  pageSize != 0){
            PageHelper.startPage(pageNum, pageSize);
            List<LinkedHashMap> fans = this.fanMapper.queryFans(fjbh, projectid, btime, etime,uid);
            PageInfo<Fan> pageInfo = new PageInfo(fans);
            return new PageData(fans, pageInfo.getTotal());
        }else{
            List<LinkedHashMap> fans = this.fanMapper.queryFans(fjbh, projectid, btime, etime,uid);
            return new PageData(fans, 0);
        }
    }





    public PageData queryFansForDownExcel(int pageNum, int pageSize, String fjbh, String projectid, String btime, String etime,String uid) {
        if(pageNum != 0 &&  pageSize != 0){
            PageHelper.startPage(pageNum, pageSize);
            List<LinkedHashMap> fans = this.fanMapper.queryFansForDownExcel(fjbh, projectid, btime, etime,uid);
            PageInfo<Fan> pageInfo = new PageInfo(fans);
            return new PageData(fans, pageInfo.getTotal());
        }else{
            List<LinkedHashMap> fans = this.fanMapper.queryFansForDownExcel(fjbh, projectid, btime, etime,uid);
            return new PageData(fans, 0);
        }
    }








    //根据ID返回fan
    public Fan queryFanById(Fan fan) {
        return this.fanMapper.selectOne(fan);
    }


    //更新风机状态
    public boolean updateFans(Fan fan) {
        int i = this.fanMapper.updateByPrimaryKeySelective(fan);
        return i > 0;
    }

    public int queryFanNumByFcode(String fcode,String attrProjectId) {
        return this.fanMapper.queryFanNumByFcode(fcode,attrProjectId);
    }

   public HashMap queryFenMessageById(String fid) {
       return  this.fanMapper.queryFensById(fid);
   }

   public boolean deleteFenById(String fid){
        Fan fan = new Fan().setId(fid);
       int i = this.fanMapper.deleteByPrimaryKey(fan);
       return  i>0;
   }

   public boolean deleteFenById(Fan fan){
       int i = this.fanMapper.deleteByPrimaryKey(fan);
       return  i>0;
   }


   public  List<Fan> queryFansByProjectId(String pid){
        return  this.fanMapper.queryFansByProjectId(pid);
   }

   public  PageData queryYqFansByPid(int pageNum, int pageSize,String pid,String queryStr){
       PageHelper.startPage(pageNum, pageSize);
       List<LinkedHashMap> yqFans = this.fanMapper.queryYqFansByPid(pid,queryStr);
       PageInfo pageInfo = new PageInfo(yqFans);
       return new PageData(yqFans, pageInfo.getTotal());
   }




    public  PageData queryYqFansByPidForDownExcel(int pageNum, int pageSize,String pid,String queryStr){

        if(pageNum != 0 &&  pageSize != 0){
            PageHelper.startPage(pageNum, pageSize);
            List<LinkedHashMap> yqFans = this.fanMapper.queryYqFansForDownExcel(pid,queryStr);
            PageInfo pageInfo = new PageInfo(yqFans);
            return new PageData(yqFans, pageInfo.getTotal());
        }else{
            List<LinkedHashMap> yqFans = this.fanMapper.queryYqFansForDownExcel(pid,queryStr);
            return new PageData(yqFans, 0);
        }









    }

    //查看即将逾期的风机信息
    public  PageData queryJjYqFansByPid(int pageNum, int pageSize,String pid,String queryStr1,String queryStr2){
        PageHelper.startPage(pageNum, pageSize);
        List<LinkedHashMap> jjyqFans = this.fanMapper.queryJjYqFansByPid(pid,queryStr1,queryStr2);
        PageInfo pageInfo = new PageInfo(jjyqFans);
        return new PageData(jjyqFans, pageInfo.getTotal());
    }



    public  PageData queryJjYqFansForDownExcel(int pageNum, int pageSize,String pid,String queryStr1,String queryStr2){
        if(pageNum != 0 &&  pageSize != 0){
            PageHelper.startPage(pageNum, pageSize);
            List<LinkedHashMap> jjyqFans = this.fanMapper.queryJjYqFansForDownExcel(pid,queryStr1,queryStr2);
            PageInfo pageInfo = new PageInfo(jjyqFans);
            return new PageData(jjyqFans, pageInfo.getTotal());
        }else {
            List<LinkedHashMap> jjyqFans = this.fanMapper.queryJjYqFansForDownExcel(pid,queryStr1,queryStr2);
            return new PageData(jjyqFans, 0);
        }

    }



}
